<template>
  <div class="container">
    <div class="top">
      <div class="top1">
        <img :src="backPath" alt="背景图片" class="backImg">
        <div class="upload">
          <el-upload
            class="upload-demo"
            drag
            action="#"
            :before-upload="beforeUpload"
            accept=".jpg, .jpeg, .png"
            multiple>
            <div class="el-upload__text" style="font-size: 18px;">
              <i class="el-icon-link"></i>
              <span>上传背景图</span>
            </div>
          </el-upload>
        </div>
        <div class="address">
          <i class="el-icon-location-outline"></i>
          <span>Ip地址 {{ userDetail.address }}</span>
        </div>
        <div class="top2">
          <img :src="avatarPath" alt="头像" class="avator"/>
          <div class="info">
            <h2>{{userDetail.name}}</h2>
            <span style="font-size: 14px;">{{ level }}</span>
            <ul class="clearfix ">
              <li class="left">钓龄: {{ userDetail.fishage }} 年</li>
              <li class="left">{{ userDetail.sex == 0 ? '女':'男' }}</li>
              <li class="left">Email:{{ userDetail.email }}</li>
            </ul>
          </div>
          <div class="changeInfoBtn">
            <el-button type="primary" size="small" @click="dialogFormVisible = true" v-show="userDetail.fishage > 5">发表高手秘籍</el-button>
            <el-button type="primary" size="small" @click="isShowChangeInfo = true">编辑个人资料</el-button>
          </div>
        </div>
      </div>
    </div>
    <hr style="margin-top: 150px;"/>
    <Main></Main>
    <ChangeInfo v-show="isShowChangeInfo" @changeIsShow="changeIsShow"/>
    <!-- 高手秘籍的发布 -->
    <el-dialog title="发表你的专属秘籍" :visible.sync="dialogFormVisible">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
        <el-form-item label="输入题目" :label-width="formLabelWidth" prop="title">
          <el-input v-model="ruleForm.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="类型" :label-width="formLabelWidth" prop="type">
            <el-select v-model="ruleForm.type" placeholder="请选择" :style="{width: '100%'}">
                <el-option v-for="(item) in typeList" :key="item.value" :label="item.label" :value="item.value" :disabled="!!item.disabled"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="正文内容" :label-width="formLabelWidth" prop="content">
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4}"
            placeholder="请输入内容"
            v-model="ruleForm.content">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
          <el-button @click="dialogFormVisible = false">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import Main from './main.vue'
import ChangeInfo from '../../components/changeInfo.vue'
import { mapState,mapMutations,mapActions,mapGetters} from 'vuex'
import { ChangeBg ,UserInfo,PublishSecret} from '@/request/api'
export default {
  data(){
    return{
      formLabelWidth: '120px',
      avatarPath:'',
      backPath:'',
      isShowChangeInfo:false,
      dialogFormVisible:false,
      secretForm:{
        secret:{
          title:'',
          type:'',
          content:''
        }
      },
      ruleForm:{
        title:'',
        type:'',
        content:''
      },
      typeList: [{
                    label: '线组搭配',
                    value: '线组搭配',
                    disabled: false
                }, {
                    label: '浮漂选取',
                    value: '浮漂选取',
                    disabled: false
                }, {
                    label: '钓点选取',
                    value: '钓点选取',
                    disabled: false
                }, {
                    label: '调漂技巧',
                    value: '调漂技巧',
                    disabled: false
                }, {
                    label: '其他',
                    value: '其他',
                    disabled: false
      }],
      rules: {
        title: [
            { required: true, message: '请输入秘籍题目', trigger: 'blur' },
            { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择文章类型', trigger: 'change' }
        ],
        content: [
          { required: true, message: '请输入秘籍内容', trigger: 'blur' },
        ]
      }
    }
  },
  components:{
    Main,ChangeInfo
  },
  computed:{
    ...mapState({
      userDetail:(state) => state.userInfo.userDetail
    }),
    ...mapGetters({
      level:'userInfo/level'
    })
  },
  methods:{
    ...mapMutations({
      setUserDetail:'userInfo/setUserDetail',
    }),
    ...mapActions({
      asyncChangeIsShowToast:'toastStatus/asyncChangeIsShowToast'
    }),
    async beforeUpload(file){
      const formData = new FormData();
      formData.append('file', file);
      // 发送请求
      let res = await ChangeBg(formData)
      // 如果成功
      if(res.data.state == 200){
        this.getUserDetails()
      // 提示
      this.asyncChangeIsShowToast({
          msg:'修改成功',
          type:'success'
        })
      }else{
        // 提示
      this.asyncChangeIsShowToast({
          msg:'修改失败',
          type:'error'
        })
      }
    },
    // 重置userInfoDetail
    async getUserDetails(){
        let res = await UserInfo()
        if(res.data.state == 200){
            // 查询成功
            this.setUserDetail(res.data.data)
        }else{
            console.log("失败了")
        }
    },
    // 控制遮罩
    changeIsShow(isShow){
      console.log("触发了")
      this.isShowChangeInfo = isShow
    },
    async handleSubmit(){
      // 发送请求
      let res = await PublishSecret(this.secretForm.secret)
      if(res.data.state == 200){
        this.asyncChangeIsShowToast({
          type:'success',
          msg:'发布成功'
        })
        this.secretForm.secret = ''
        this.dialogFormVisible = false
      }else{
        this.asyncChangeIsShowToast({
          type:'error',
          msg:'发布失败,请重试'
        })
      }
    },
    submitForm(formName) {
        this.$refs[formName].validate(async (valid) => {
          if (valid) {
            // 发送请求
            // 发送请求
            let res = await PublishSecret(this.ruleForm)
            if(res.data.state == 200){
              this.asyncChangeIsShowToast({
                type:'success',
                msg:'发布成功'
              })
              this.secretForm.secret = ''
              this.dialogFormVisible = false
            }else{
              this.asyncChangeIsShowToast({
                type:'error',
                msg:'发布失败,请重试'
              })
            }
          } else {
            return false;
          }
        });
    },
  },
  mounted(){
    this.avatarPath = require('../../assets/avator/'+this.userDetail.avator)
    this.backPath = require('../../assets/backImg/'+this.userDetail.backimg)
  }
}
</script>

<style scope>
.backImg{
  width: 1200px;
  height: 200px;
}
.top{
  margin-bottom: 20px;
}
 .top1{
   width: 1200px;
   height: 200px;
   /* background-image: url('../../assets/images/b.jpg'); */
   repeat: no-repeat; 
   background-size: 100% 100%;
   z-index: 1;
   position: relative;
 }
 .top2{
  width: 1200px;
  height: 150px;
  z-index: 2;
  position: absolute;
  top: 90%;
  box-sizing: border-box;
 }
 .upload{
   position: absolute;
   right: 50px;
   top: 50px;
 }
 .el-upload-dragger{
  width: 130px !important;
  height: 30px !important; 
  line-height: 30px !important;
  font-size: 20px !important;
 }

 .avator{
  position: absolute;
  top: -50px;
  left: 50px;
  width: 150px;
  height: 150px;
  border: 7px solid white;
  border-radius: 5px;
 }
 .info{
   position: absolute;
   top: 20px;
   left: 250px;
   font-size: 16px;
 }
 .info ul{
  list-style: none;
  margin: 0;  
  padding: 0;
  margin-top: 20px;
 }
 .info ul li{
   margin-right: 10px;
 }
 .changeInfoBtn{
   position: absolute;
   bottom: 50px;
   right: 100px;
 }
 .address{
  position: absolute;
  background-color: gray;
  border: 1px solid white;
  border-radius: 5px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  right: 70px;
  bottom: 30px;
  padding: 0 5px;
  opacity: 0.5;
  color: white;
  font-weight: bold;
 }
</style>
